package org.jaaksi.view.filterbar.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Fcy on 2015/10/31.
 */
abstract class BaseListAdapter<D> extends ArrayAdapter<D>
{
	protected List<D> mData = new ArrayList<>();
	protected LayoutInflater mInflate;
	protected Context mContext;

	public BaseListAdapter(Context context)
	{
		super(context, android.R.layout.simple_list_item_1);
		mInflate = (LayoutInflater) context
		        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		mContext = context;
	}

	public List<D> getData()
	{
		return mData;
	}

	public void setData(List<D> list)
	{
		this.mData = list;
		notifyDataSetChanged();
	}

	@Override
	public D getItem(int position)
	{
		return mData.get(position);
	}

	@Override
	public long getItemId(int position)
	{
		return position;
	}

	public abstract View getView(int position, View convertView,
	        ViewGroup parent);

	@Override
	public int getCount()
	{
		return mData == null ? 0 : mData.size();
	}

	public void hideLastPositionView(int position, View view)
	{
		view.setVisibility(
		        position == getCount() - 1 ? View.GONE : View.VISIBLE);
	}
}
